package ro.gateway.aida.autodates;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import ro.gateway.aida.db.DBPersistenceManager;
import ro.gateway.aida.db.PersistenceToken;
import ro.gateway.aida.obj.db.ActivityStatusDB;

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: Jul 6, 2004
 * Time: 5:24:42 PM
 * To change this template use File | Settings | File Templates.
 */
public class ActivityForStatus$Proxy$DB extends DBPersistenceManager {
	private ActivityForStatus$Proxy$DB(PersistenceToken token) {
		super(token);
	}

	public static ActivityForStatus$Proxy$DB getManager(PersistenceToken token) {
		return new ActivityForStatus$Proxy$DB(token);
	}

	public void updateStatus(ActivityForStatus$Proxy activityProxy)
		  throws SQLException {

		if (activityProxy.getStatus().getId() == -1)
			throw new RuntimeException("Illegal call of ActivityForStatus$Proxy$DB::updateStatus");

		Connection con = getConnection();
		try {
			PreparedStatement ps = con.prepareStatement(UPDATE);
			ps.setLong(1, activityProxy.getStatus().getId());
			ps.setLong(2, activityProxy.getId());
			ps.executeUpdate();
			ps.close();
		} catch (SQLException sqlex) {
			throw sqlex;
		} finally {
			con.close();
		}
	}

	public void updateStatusForActivities(ActivityForStatus$Proxy activityProxy[])
		  throws SQLException {

                 if (activityProxy == null){
				  return;
			  }

		Connection con = getConnection();
		try {
			PreparedStatement ps = con.prepareStatement(UPDATE);
			for (int i = 0; i < activityProxy.length; i++) {
				if (activityProxy[i].getStatus().getId() == -1)
					throw new RuntimeException("Illegal call of ActivityForStatus$Proxy$DB::updateStatus");
				ps.clearParameters();
				ps.setLong(1, activityProxy[i].getStatus().getId());
				ps.setLong(2, activityProxy[i].getId());
				ps.executeUpdate();
			}
			ps.close();
		} catch (SQLException sqlex) {
			throw sqlex;
		} finally {
			con.close();
		}
	}

	public ActivityForStatus$Proxy getById(long id)
		  throws SQLException {
		Connection con = getConnection();
		ActivityForStatus$Proxy result = null;

		try {
			PreparedStatement ps = con.prepareStatement(GET_BY_ID);
			ps.setLong(1, id);

			ResultSet rs = ps.executeQuery();

			if (rs.next()) {
				result = new ActivityForStatus$Proxy();

				result.setId(id);

				Date zadate = rs.getDate(1);
				result.setDate_aa((zadate != null) ? zadate.getTime() : (-1));
				if (rs.wasNull()) result.setDate_aa(-1);

				zadate = rs.getDate(2);
				result.setDate_as((zadate != null) ? zadate.getTime() : (-1));
				if (rs.wasNull()) result.setDate_as(-1);

				zadate = rs.getDate(3);
				result.setDate_ae((zadate != null) ? zadate.getTime() : (-1));
				if (rs.wasNull()) result.setDate_ae(-1);

				long status = rs.getLong(4);

				if (rs.wasNull()) {
					status = -1;
					result.setStatus(null);
				} else
					result.setStatus(ActivityStatusDB
						  .getManager(db_token).getById(status));

				result.setStatus_automation_on(rs.getInt(5) != 0);
			}

			rs.close();

			ps.close();
		} catch (SQLException sqlEx) {
			throw sqlEx;
		} finally {
			con.close();
		}

		return result;
	}

	public ActivityForStatus$Proxy[] getAll() throws SQLException {
		ArrayList items = new ArrayList();

		Connection con = getConnection();
		try {
			PreparedStatement ps = con.prepareStatement(GET_ALL);
			ResultSet rs = ps.executeQuery();
			while (rs.next()) {
				ActivityForStatus$Proxy item = new ActivityForStatus$Proxy();

				Date zadate = rs.getDate(1);
				item.setDate_aa((zadate != null) ? zadate.getTime() : (-1));
				if (rs.wasNull()) item.setDate_aa(-1);

				zadate = rs.getDate(2);
				item.setDate_as((zadate != null) ? zadate.getTime() : (-1));
				if (rs.wasNull()) item.setDate_as(-1);

				zadate = rs.getDate(3);
				item.setDate_ae((zadate != null) ? zadate.getTime() : (-1));
				if (rs.wasNull()) item.setDate_ae(-1);

				long status = rs.getLong(4);

				if (rs.wasNull()) {
					status = -1;
					item.setStatus(null);
				} else
					item.setStatus(ActivityStatusDB
						  .getManager(db_token).getById(status));

				item.setStatus_automation_on(rs.getInt(5) != 0);
				item.setId(rs.getLong(6));

				items.add(item);
			}
			rs.close();
			ps.close();
		} catch (SQLException sqlEx) {
			throw sqlEx;
		} finally {
			con.close();
		}

		if (items.size() > 0) {
			ActivityForStatus$Proxy[] result = new ActivityForStatus$Proxy[items.size()];
			items.toArray(result);
			return result;
		} else {
			return null;
		}
	}

	private static final String GET_BY_ID =
		  "SELECT date_aa, date_as, date_ae, harm_status, autostatuson " +
		  "FROM activities WHERE id = ?";
	private static final String GET_ALL =
		  "SELECT date_aa, date_as, date_ae, harm_status, autostatuson, id " +
		  "FROM activities WHERE autostatuson IS NOT NULL AND autostatuson!=0";
	private static final String UPDATE =
		  "UPDATE activities SET harm_status = ? WHERE id = ?";
}
